﻿using Server.Business;
using Server.Business.proto;
using Server.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LightingWebService
{
    public class LightingFacade
    {
        public byte[] GetStaticResource()
        {
            return ResourceProvider.ResArray.ToByteArray();
        }

        public bool SignLable(string name)
        {
            ProdBusiness os = new ProdBusiness();
            os.SignLable(name);
            return true;
        }

        public bool UnSignLable(string name)
        {
            ProdBusiness os = new ProdBusiness();
            os.UnSignLable(name);
            return true;
        }

        public byte[] SearchProdName()
        {
            ProdBusiness os = new ProdBusiness();
            return os.SearchProdName();
        }

        public byte[] Login(string name, string pwd)
        {
            UserBusiness ub = new UserBusiness();
            UserDB user = ub.Login(name, pwd);
            if (user != null)
            {
                UserConverter converter = new UserConverter();
                return converter.To(user).ToByteArray();
            }

            return null;
        }

        public byte[] SearchProdList(byte[] conditions)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchProdList(conditions);
        }

        public byte[] GetUsers()
        {
            UserArray.Builder builder = new UserArray.Builder();
            UserConverter converter = new UserConverter();

            UserBusiness us = new UserBusiness();
            foreach (UserDB user in us.GetUsers())
            {
                builder.UserList.Add(converter.To(user));
            }
            return builder.Build().ToByteArray();
        }

        public string AddUser(byte[] bytes)
        {
            UserBusiness os = new UserBusiness();
            return os.AddUser(User.ParseFrom(bytes));
        }

        public bool UpdateUser(byte[] bytes)
        {
            UserBusiness os = new UserBusiness();
            return os.UpdateUser(User.ParseFrom(bytes));
        }

        public bool DeleteUser(string id)
        {
            UserBusiness os = new UserBusiness();
            return os.DeleteUser(id);
        }

        public string AddProd(byte[] bytes)
        {
            ProdBusiness ob = new ProdBusiness();
            return ob.AddProd(Prod.ParseFrom(bytes));
        }

        public bool UpdateProd(byte[] bytes)
        {
            ProdBusiness ob = new ProdBusiness();
            return ob.UpdateProd(Prod.ParseFrom(bytes));
        }

        public bool DeleteProd(string id)
        {
            ProdBusiness ob = new ProdBusiness();
            return ob.DeleteProd(id);
        }

        public byte[] SearchProdClass(byte[] conditions)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchProdClass(conditions);
        }

        public byte[] SearchProdSeries(byte[] conditions)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchProdSeries(conditions);
        }

        public byte[] SearchPrimaryProdInfo(string id)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchPrimaryProdInfo(id);
        }

        public string SearchPrimaryProdInfoToPDF(string id)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchPrimaryProdInfoToPDF(id);
        }

        public string SearchPrimaryProdInfoToExcel(string id)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchPrimaryProdInfoToExcel(id);
        }

        public byte[] SearchProdInfo(string id)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchProdInfo(id);
        }

        public string SearchProdInfoToPDF(string id)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchProdInfoToPDF(id);
        }

        public string SearchProdInfoToExcel(string id)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchProdInfoToExcel(id);
        }

        public byte[] SearchRecommendedProd(byte[] conditions)
        {
            return null;
        }

        public byte[] SearchRelations(string id)
        {
            ProdBusiness pb = new ProdBusiness();
            return pb.SearchRelations(id);
        }

        public void Log(string action, string info)
        {
            LogService.Log(new LogDB() { Action = action, Info = info });
        }

        public bool BatchDeleteProd(byte[] bytes)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.BatchDeleteProd(bytes);
        }

        public string CustomExportToExcel(byte[] bytes)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.CustomExportToExcel(bytes);
        }

        public string CustomExportToPDF(byte[] bytes)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.CustomExportToPDF(bytes);
        }

        public byte[] SearchNewProds()
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchNewProds();
        }

        public string SearchProdListToPDF(byte[] conditions)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchProdListToPDF(conditions);
        }

        public string SearchProdListToExcel(byte[] conditions)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchProdListToExcel(conditions);
        }

        public string SearchProdSeriesToPDF(byte[] conditions)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchProdSeriesToPDF(conditions);
        }

        public string SearchProdSeriesToExcel(byte[] conditions)
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchProdSeriesToExcel(conditions);
        }
        
        public string SearchNewProdsToPDF()
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchNewProdsToPDF();
        }

        public string SearchNewProdsToExcel()
        {
            ProdBusiness sb = new ProdBusiness();
            return sb.SearchNewProdsToExcel();
        }

    }
}
